/*
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * htap_cfg.h
 * Holds global configuration for the HTAP module.
 *
 * IDENTIFICATION
 * src/gausskernel/storage/htap/htap_cfg.h
 *
 * -------------------------------------------------------------------------
 */

#ifndef HTAP_CFG_H
#define HTAP_CFG_H

#include <set>
#include <climits>
#include <cstdint>

#include "htap_logger.h"
#include "mot_configuration.h"
#include "mot_log.h"

namespace htap {
/**
 * @class MOTConfiguration
 * @brief Holds global configuration for the MOT storage engine.
 */
class HTAPCfg : public MOT::ConfigBase {
public:
    HTAPCfg();
    ~HTAPCfg() override {}

    /** @brief Loads configuration from main configuration. */
    void LoadConfig();

    /** @brief React to a notification that configuration changed. */
    void OnConfigChange() override;

    /* ******************************************************************** */
    // General configuration
    /* ******************************************************************** */
    /** @var Specifies whether HTAP is enabled. */
    bool m_enableHtap;

    /* ******************************************************************** */
    // Log configuration
    /* ******************************************************************** */
    /** @var Specifies which kind of log format to use (internal or external). */
    MOT::LogFormat m_logFormat;

    /* ******************************************************************** */
    // Statistics configuration
    /* ******************************************************************** */
    /** @brief Enables statistics printing. */
    bool m_enableStats;

    /* * Statistics printing period seconds. */
    uint64_t m_statPrintPeriodSeconds;

    /* * Full statistics printing period in seconds. */
    uint64_t m_statPrintFullPeriodSeconds;

    /** @var Specifies whether enable process statistics printing to log. */
    bool m_enableProcessStatistics;

    /** @var Specifies whether enable system statistics printing to log. */
    bool m_enableSystemStatistics;

    /** @var Specifies whether enable HTAP hook statistics printing to log. */
    bool m_enableModifyStatistics;

    /** @var Specifies whether enable HTAP delta statistics printing to log. */
    bool m_enableDeltaStatistics;

    /** @var Specifies whether enable HTAP flush statistics printing to log. */
    bool m_enableFlushStatistics;

    bool m_enableSegmentTree;

    bool m_enableFlush;

    bool m_enableFlushPrintStats;

    bool m_enabelHTAPXLogRedoer;

    /* ******************************************************************** */
    // Flush configuration
    /* ******************************************************************** */
    /** @var Flush timeout in milliseconds. */
    uint64_t m_flushTimeoutMillis;

    /* ******************************************************************** */
    // htap xlog redoer configuration
    /* ******************************************************************** */
    /** @var Flush timeout in milliseconds. */
    uint64_t m_htapXLogRedoTimeoutMillis;

    /* * ------------------ Default General Configuration ------------ */
    /** @var Default enable HTAP. */
    static constexpr bool DEFAULT_ENABLE_HTAP = true;

    /* * ------------------ Default Log Configuration ------------ */
    /** @var Default log format. */
    static constexpr MOT::LogFormat DEFAULT_LOG_FORMAT = MOT::LOG_FORMAT_INTERNAL;

    /* * ------------------ Default Statistics Configuration ------------ */
    /** @var Default enable statistics printing. */
    static constexpr bool DEFAULT_ENABLE_STATS = false;

    /** @var Default statistics printing period in seconds. */
    static constexpr const char *DEFAULT_STATS_PRINT_PERIOD = "1 minutes";
    static constexpr uint64_t DEFAULT_STATS_PRINT_PERIOD_SECONDS = 60;
    static constexpr uint64_t MIN_STATS_PRINT_PERIOD_SECONDS = 1;
    static constexpr uint64_t MAX_STATS_PRINT_PERIOD_SECONDS = 86400; // 1 day

    /** @var Default full statistics printing period in seconds. */
    static constexpr const char *DEFAULT_FULL_STATS_PRINT_PERIOD = "5 minutes";
    static constexpr uint64_t DEFAULT_FULL_STATS_PRINT_PERIOD_SECONDS = 300;

    /** @var Default enable process statistics printing. */
    static constexpr bool DEFAULT_ENABLE_PROCESS_STAT_PRINT = false;

    /** @var Default enable system statistics printing. */
    static constexpr bool DEFAULT_ENABLE_SYSTEM_STAT_PRINT = false;

    /** @var Default enable HTAP hook statistics printing. */
    static constexpr bool DEFAULT_ENABLE_HOOK_STAT_PRINT = false;

    /** @var Default enable HTAP delta statistics printing. */
    static constexpr bool DEFAULT_ENABLE_DELTA_STAT_PRINT = false;

    /** @var Default enable HTAP flush statistics printing. */
    static constexpr bool DEFAULT_ENABLE_FLUSH_STAT_PRINT = false;

    static constexpr bool DEFAULT_ENABLE_SEGMENT_TREE = false;

    /* * ------------------ Default Flush Configuration ------------ */
    static constexpr const char *DEFAULT_FLUSH_TIMEOUT = "5 ms";
    static constexpr const char *DEFAULT_HTAP_XLOG_REDOER_TIMEOUT = "5 ms";
    static constexpr uint64_t DEFAULT_FLUSH_TIMEOUT_MILLIS = 5;
    static constexpr uint64_t DEFAULT_HTAP_XLOG_REDOER_TIMEOUT_MILLIS = 5;
    static constexpr uint64_t MIN_FLUSH_TIMEOUT_MILLIS = 1;
    static constexpr uint64_t MAX_FLUSH_TIMEOUT_MILLIS = 60000; // 1 minute
    static constexpr bool DEFAULT_ENABLE_FLUSH = true;
    static constexpr bool DEFAULT_ENABLE_HTAP_XLOG_REDOER = true;
    static constexpr bool DEFAULT_ENABLE_FLUSH_PRINT_STATS = false;

private:
    void ConfigureMOTEngine();
};
} // namespace htap

#endif /* HTAP_CFG_H */
